package com.farmer.expert.controller;

import com.farmer.common.entity.AgriExpert;
import com.farmer.common.result.Result;
import com.farmer.expert.service.ExpertService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 农业专家控制器
 */
@Slf4j
@RestController
@RequestMapping("/expert")
@Tag(name = "农业专家管理", description = "农业专家相关接口")
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    /**
     * 获取专家列表（分页）
     */
    @GetMapping("/list")
    @Operation(summary = "获取专家列表", description = "分页获取专家列表，支持条件筛选")
    public Result<?> getExpertList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "专业领域") @RequestParam(required = false) String specialties,
            @Parameter(description = "可用状态") @RequestParam(required = false) String availableStatus,
            @Parameter(description = "是否认证") @RequestParam(required = false) Integer isVerified,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword) {
        return expertService.getExpertList(pageNum, pageSize, specialties, availableStatus, isVerified, keyword);
    }

    /**
     * 根据ID获取专家详情
     */
    @GetMapping("/{expertId}")
    @Operation(summary = "获取专家详情", description = "根据专家ID获取详细信息")
    public Result<?> getExpertById(@Parameter(description = "专家ID") @PathVariable Long expertId) {
        return expertService.getExpertById(expertId);
    }

    /**
     * 添加农业专家
     */
    @PostMapping
    @Operation(summary = "添加农业专家", description = "创建新的农业专家")
    public Result<?> addExpert(@RequestBody AgriExpert expert) {
        return expertService.addExpert(expert);
    }

    /**
     * 更新专家信息
     */
    @PutMapping
    @Operation(summary = "更新专家信息", description = "更新现有专家信息")
    public Result<?> updateExpert(@RequestBody AgriExpert expert) {
        return expertService.updateExpert(expert);
    }

    /**
     * 删除专家
     */
    @DeleteMapping("/{expertId}")
    @Operation(summary = "删除专家", description = "删除指定的专家")
    public Result<?> deleteExpert(@Parameter(description = "专家ID") @PathVariable Long expertId) {
        return expertService.deleteExpert(expertId);
    }

    /**
     * 更新专家状态
     */
    @PutMapping("/{expertId}/status")
    @Operation(summary = "更新专家状态", description = "更新专家启用/禁用状态")
    public Result<?> updateExpertStatus(
            @Parameter(description = "专家ID") @PathVariable Long expertId,
            @Parameter(description = "新状态") @RequestParam Integer status) {
        return expertService.updateExpertStatus(expertId, status);
    }

    /**
     * 更新专家可用状态
     */
    @PutMapping("/{expertId}/available-status")
    @Operation(summary = "更新专家可用状态", description = "更新专家当前可用状态")
    public Result<?> updateAvailableStatus(
            @Parameter(description = "专家ID") @PathVariable Long expertId,
            @Parameter(description = "可用状态") @RequestParam String availableStatus) {
        return expertService.updateAvailableStatus(expertId, availableStatus);
    }

    /**
     * 专家认证
     */
    @PutMapping("/{expertId}/verify")
    @Operation(summary = "专家认证", description = "对专家进行认证或取消认证")
    public Result<?> verifyExpert(
            @Parameter(description = "专家ID") @PathVariable Long expertId,
            @Parameter(description = "是否认证") @RequestParam Integer isVerified) {
        return expertService.verifyExpert(expertId, isVerified);
    }

    /**
     * 获取推荐专家列表
     */
    @GetMapping("/recommended")
    @Operation(summary = "获取推荐专家", description = "获取推荐的专家列表")
    public Result<?> getRecommendedExperts(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        return expertService.getRecommendedExperts(limit);
    }

    /**
     * 根据专业领域获取专家
     */
    @GetMapping("/specialties/{specialties}")
    @Operation(summary = "按专业领域获取专家", description = "根据专业领域获取专家列表")
    public Result<?> getExpertsBySpecialties(
            @Parameter(description = "专业领域") @PathVariable String specialties,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return expertService.getExpertsBySpecialties(specialties, pageNum, pageSize);
    }

    /**
     * 增加咨询次数
     */
    @PutMapping("/{expertId}/consultation/increase")
    @Operation(summary = "增加咨询次数", description = "增加专家咨询次数统计")
    public Result<?> increaseConsultationCount(@Parameter(description = "专家ID") @PathVariable Long expertId) {
        return expertService.increaseConsultationCount(expertId);
    }

    /**
     * 增加授课次数
     */
    @PutMapping("/{expertId}/course/increase")
    @Operation(summary = "增加授课次数", description = "增加专家授课次数统计")
    public Result<?> increaseCourseCount(@Parameter(description = "专家ID") @PathVariable Long expertId) {
        return expertService.increaseCourseCount(expertId);
    }

    /**
     * 更新专家评分
     */
    @PutMapping("/{expertId}/rating")
    @Operation(summary = "更新专家评分", description = "更新专家服务评分")
    public Result<?> updateRating(
            @Parameter(description = "专家ID") @PathVariable Long expertId,
            @Parameter(description = "评分") @RequestParam Double rating) {
        return expertService.updateRating(expertId, rating);
    }

    /**
     * 获取专家统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取专家统计", description = "获取专家相关统计信息")
    public Result<?> getExpertStatistics() {
        return expertService.getExpertStatistics();
    }
} 